<?php

namespace app\admin\server\group;

use app\admin\model\group\GroupGoodsModel;
use app\admin\model\group\GroupOrderModel;
use app\admin\ServerBase;
use app\admin\validate\group\GroupGoodsVal;
use app\api\model\group\GroupOrderApiModel;
use think\db\exception\DbException;
use think\Request;

class GroupGoodsServer extends ServerBase
{

    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var mixed
     */
    protected $validate;

    function __construct(Request $request)
    {
        $this->request = $request;
        $this->param = $request->param();
        $this->model = new GroupGoodsModel();
        $this->validate = new GroupGoodsVal();
    }

    public function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $info['buy_num'] = GroupOrderModel::getBuyNum($info['id']);
        $this->getMessageCode('adminMessage.success.select', 2);
        return $info;
    }
    function add()
    {
        $post = $this->request->post();
        if (!$this->validate->scene('create')->check($post)) {
            $this->ERROR = $this->validate->getError();
            return false;
        }
        try {
            $this->model->save($post);
            $this->getMessageCode('adminMessage.success.insert', 2);
            return true;
        } catch (\Error $error) {
            $this->ERROR = $error->getMessage();
            return false;
        } catch (DbException $dbException) {
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }

    function edit()
    {
        $id = $this->request->param('id', 0);
        $post = $this->request->post();
        $post['id'] = $id;
        if (!$this->validate->scene('update')->check($post)) {
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info = $this->model->getFindById($id);
        if (empty($info)) {
            $this->getMessageCode('adminMessage.error.update', 1);
            return false;
        }
        if($info['is_complete'] !=10){
            $this->ERROR='当前拼团商品不能修改';
            return false;
        }
        $info['num'] = $post['num'];
        $num = GroupOrderApiModel::getAdminBuyNum($info,$info['id']);
        if($num> $info['complete_num']){
            $this->ERROR='拼团人数不能超过完成人数';
            return false;
        }
        if ($info->save($post)) {
            $this->getMessageCode('adminMessage.success.update', 2);
            return true;
        } else {
            $this->getMessageCode('adminMessage.error.update', 1);
            return false;
        }
    }

    function edit_other(){
        $id = $this->request->param('id', 0);
        $info = $this->model->getFindById($id);
        if (empty($info)) {
            $this->getMessageCode('adminMessage.error.update', 1);
            return false;
        }
        if($info['is_complete'] !=10){
            $this->ERROR='当前拼团商品不能修改';
            return false;
        }
        $post =$this->request->post();
        if(isset($post['num'])){
            $info['num'] = $post['num'];
            $num = GroupOrderApiModel::getAdminBuyNum($info,$info['id']);
            if($num> $info['complete_num']){
                $this->ERROR='拼团人数不能超过完成人数';
                return false;
            }
        }
        $info->save($post);
        $this->getMessageCode('adminMessage.success.update',2);
        return true;
    }
}